/**
 * 
 */
package dt.inlee.system.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import dt.inlee.common.dao.impl.BaseDao;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.SysMenu;

/**
 * @功能说明：系统菜单数据访问层
 * @作者： 印鲜刚
 * @创建日期： 2010-5-8 @
 */
@Repository("sysMenuDao")
public class SysMenuDao extends BaseDao {
	Logger logger = Logger.getLogger(SysMenuDao.class);

	/**
	 * 通过菜单parentId获取其所有直接子菜单集合
	 * 
	 * @param parentId
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<SysMenu> findDirectSubSysMenus(String parentId) throws FrameworkException {
		StringBuffer hql = new StringBuffer();
		hql.append("FROM SysMenu sm ");
		if (!BaseUtil.isEmpty(parentId)) {
			hql.append("WHERE sm.sysMenu.id = ? ");
			hql.append("ORDER BY sm.id,sm.orderNo ASC ");
			return this.hibernateTemplate.find(hql.toString(), parentId);
		} else {
			hql.append("WHERE sm.sysMenu.id is null ");
			hql.append("ORDER BY sm.id,sm.orderNo ASC ");
			return this.hibernateTemplate.find(hql.toString());
		}
	}

	/**
	 * 根据条件查询菜单实体
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysMenu> findAllSysMenus() throws FrameworkException {
		return this.findAll(SysMenu.class);
	}

	/**
	 * 获取头部菜单列表
	 * 
	 * @param parentId
	 * @param sysUserId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysMenu> findSubSysMenusByLoginUserId(String parentId, int sysUserId) throws FrameworkException {
		StringBuffer hql = new StringBuffer();
		hql.append("SELECT DISTINCT sm FROM SysMenu sm LEFT JOIN sm.sysRoles so LEFT JOIN so.sysUsers su ");
		hql.append("WHERE sm.sysMenu.id = ? ");
		hql.append("AND su.id = ? ORDER BY sm.orderNo ASC ");
//		hibernateTemplate.find(queryString, value)
		return this.hibernateTemplate.find(hql.toString(), new Object[] { parentId, sysUserId});
	}

	/**
	 * 子节点顺序号 批量更新
	 * 
	 * @param List
	 * @throws FrameworkException
	 */
	public void updateAll(List<SysMenu> list, Integer value) throws FrameworkException {
		for (SysMenu sysMenu : list) {
			sysMenu.setOrderNo(sysMenu.getOrderNo() + value);
			this.update(sysMenu);
		}
	}

	/**
	 * 获取指定节点的子节点
	 * 
	 * @param nodeId
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<SysMenu> getSubAllNode(String nodeId) throws FrameworkException {
		String hql = "FROM SysMenu sm WHERE sm.sysMenu.id=?";
		return this.hibernateTemplate.find(hql, nodeId);
	}

	/**
	 * 根据菜单ID及名字查找菜单
	 * @param id
	 * @param name
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public SysMenu findSysMenuByIdAndName(String id, String name) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(SysMenu.class);
		if (!BaseUtil.isEmpty(name)) {
			criteria.add(Restrictions.eq("name", name));
		}
		criteria.add(Restrictions.ne("id", id));
		List<SysMenu> menus = criteria.list();
		if (!BaseUtil.isEmpty(menus) && menus.size() > 0)
			return menus.get(0);
		return null;
	}

	/**
	 * 根据菜单名字查找菜单
	 * @param name
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public SysMenu findSysMenuByName(String name) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(SysMenu.class);
		if (!BaseUtil.isEmpty(name)) {
			criteria.add(Restrictions.eq("name", name));
		}
		List<SysMenu> menus = criteria.list();
		if (!BaseUtil.isEmpty(menus) && menus.size() > 0)
			return menus.get(0);
		return null;
	}

}
